package com.ruoyi.supermarket.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.enums.AuditStatusType;
import com.ruoyi.supermarket.service.ShopService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.supermarket.domain.ProductRes;
import com.ruoyi.supermarket.service.ProductResService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品资源Controller
 * 
 * @author liqiwei
 * @date 2023-11-15
 */
@RestController
@RequestMapping("/supermarket/productRes")
public class ProductResController extends BaseController
{
    @Autowired
    private ProductResService productResService;

    @Autowired
    private ShopService shopService;
    /**
     * 查询商品资源列表
     */
    @PreAuthorize("@ss.hasPermi('supermarket:productRes:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductRes productRes)
    {
        //店铺用户只能查询自己的数据
        if (hasShop()) {
            productRes.setShopId(shopService.selectShopByUserId(getUserId()));
        }
        startPage();
        List<ProductRes> list = productResService.selectProductResList(productRes);
        return getDataTable(list);
    }

    /**
     * 查询商品资源列表
     */
    @PreAuthorize("@ss.hasPermi('supermarket:productRes:list')")
    @GetMapping("/query")
    public List<ProductRes> query(ProductRes productRes)
    {
        //店铺用户只能查询自己的数据
        if (hasShop()) {
            productRes.setShopId(shopService.selectShopByUserId(getUserId()));
        }
        productRes.setAuditStatus(AuditStatusType.SUCCESS.getStatus());
        return productResService.selectProductResList(productRes);
    }

    /**
     * 导出商品资源列表
     */
    @PreAuthorize("@ss.hasPermi('supermarket:productRes:export')")
    @Log(title = "商品资源", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductRes productRes)
    {
        List<ProductRes> list = productResService.selectProductResList(productRes);
        ExcelUtil<ProductRes> util = new ExcelUtil<ProductRes>(ProductRes.class);
        util.exportExcel(response, list, "商品资源数据");
    }

    /**
     * 获取商品资源详细信息
     */
    @PreAuthorize("@ss.hasPermi('supermarket:productRes:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(productResService.selectProductResById(id));
    }

    /**
     * 新增商品资源
     */
    @PreAuthorize("@ss.hasPermi('supermarket:productRes:add')")
    @Log(title = "商品资源", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductRes productRes)
    {
        //用户的店铺Id
        if (hasShop()) {
            productRes.setShopId(shopService.selectShopByUserId(getUserId()));
        }
        return toAjax(productResService.insertProductRes(productRes));
    }

    /**
     * 修改商品资源
     */
    @PreAuthorize("@ss.hasPermi('supermarket:productRes:edit')")
    @Log(title = "商品资源", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductRes productRes)
    {
        return toAjax(productResService.updateProductRes(productRes));
    }

    /**
     * 提交商品资源
     */
    @PreAuthorize("@ss.hasPermi('supermarket:productRes:commit')")
    @Log(title = "商品资源", businessType = BusinessType.GRANT)
    @PostMapping("/commit")
    public AjaxResult commit(@RequestBody ProductRes req)
    {
        return toAjax(productResService.commitProductRes(req));
    }

    /**
     * 审核商品资源
     */
    @PreAuthorize("@ss.hasPermi('supermarket:productRes:audit')")
    @Log(title = "商品资源", businessType = BusinessType.GRANT)
    @PostMapping("/audit")
    public AjaxResult audit(@RequestBody ProductRes req)
    {
        return toAjax(productResService.auditProductRes(req));
    }

    /**
     * 删除商品资源
     */
    @PreAuthorize("@ss.hasPermi('supermarket:productRes:remove')")
    @Log(title = "商品资源", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(productResService.deleteProductResByIds(ids));
    }
}
